import phpFetch from "../../../Core/Net/PhpNet.ts";
import javaFetch from "../../../Core/Net/JavaNet.ts";
import {
  AccountAuthEntity,
  AccountBasicEntity,
  AccountEntity,
  VipStatusEntity,
  AccountNetModel,
  VipStatusNetModel
} from "../Entity/account_entity.ts";
import { isJavaNetSuccess, isPhpNetSuccess } from "../../../Core/Net/NetParams.ts";
import RNMMKV from "../../../Native/Module/RNMMKV.ts";

class AccountLds {
  saveAccountInfo(accountInfo: { accountInfo: AccountNetModel, vipStatus: VipStatusNetModel }) {
    this.saveAccountBasic(accountInfo.accountInfo);
    this.saveVipStatus(accountInfo.vipStatus);
  }

  getAccountInfo(): AccountEntity | null {
    const accountBasic = this.getAccountBasic();
    const vipStatus = this.getVipStatus();
    if (accountBasic == null) return null;
    return new AccountEntity(AccountBasicEntity.asEntity(accountBasic), AccountAuthEntity.asEntity(accountBasic), this.getVipStatusEntity(vipStatus));
  }

  private saveAccountBasic(account: AccountNetModel) {
    RNMMKV.saveString("account_basic", JSON.stringify(account));
  }


  private saveVipStatus(vipStatus: VipStatusEntity) {
    RNMMKV.saveString("vip_status", JSON.stringify(vipStatus));
  }

  private getAccountBasic(): AccountNetModel | null {
    const accountBasicJson = RNMMKV.getString("account_basic");
    if (!accountBasicJson) return null;
    return JSON.parse(accountBasicJson) as AccountNetModel;
  }

  private getVipStatus(): VipStatusNetModel | null {
    const vipStatus = RNMMKV.getString("vip_status");
    if (!vipStatus) return null;
    return JSON.parse(vipStatus) as VipStatusNetModel;
  }

  private getVipStatusEntity(vipStatus: VipStatusNetModel | null): VipStatusEntity {
    if (vipStatus == null) return new VipStatusEntity({ status: -1, label: "" });
    return VipStatusEntity.asEntity(vipStatus);
  }
}

class AccountRds {

  private fetchPersonInfo() {
    return phpFetch<AccountNetModel>({ url: "job/member/personal" }).then(value => {
      if (isPhpNetSuccess(value)) {
        return value.data;
      } else {
        return Promise.reject(value);
      }
    });
  }

  private fetchVipEnterStatus() {
    return javaFetch<VipStatusNetModel>({ url: "member/v2/vipEnter/status", params: {} }).then(value => {
      if (isJavaNetSuccess(value)) {
        return value.data;
      } else {
        return Promise.reject(value);
      }
    });
  }

  fetchAccountInfo() {
    return Promise.all([this.fetchPersonInfo(), this.fetchVipEnterStatus()]);
  }
}

export default class AccountRep {

  private readonly accountLds: AccountLds;
  private readonly accountRds: AccountRds;

  constructor() {
    this.accountLds = new AccountLds();
    this.accountRds = new AccountRds();
  }

  getAccountInfo() {
    return Promise.resolve(this.accountLds.getAccountInfo()).then(value => {
      if (value) {
        return value;
      } else {
        this.syncAccount();
        return this.accountLds.getAccountInfo()!!;
      }
    });
  }

  syncAccount() {
    this.accountRds.fetchAccountInfo()
      .then(value => {
        this.accountLds.saveAccountInfo({ accountInfo: value[0], vipStatus: value[1] });
        return value;
      }).then(([account, vipEnterStatus]) => {
        return AccountEntity.toAccountEntity(account, vipEnterStatus);
      }
    );
  }
}
